/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.upc.sad.controller;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.TooManyListenersException;
import javax.servlet.http.HttpSession;
import pe.edu.upc.sad.dao.EmpresaAreaDao;
import pe.edu.upc.sad.dao.EmpresaAreaDaoImpl;
import pe.edu.upc.sad.dao.ReservaDao;
import pe.edu.upc.sad.dao.ReservaDaoImpl;
import pe.edu.upc.sad.dao.TipoAreaDao;
import pe.edu.upc.sad.dao.TipoAreaDaoImpl;
import pe.edu.upc.sad.model.EmpresaArea;
import pe.edu.upc.sad.model.MensajeResultado;
import pe.edu.upc.sad.model.Reserva;
import pe.edu.upc.sad.model.TipoArea;
import pe.edu.upc.sad.util.Conexion;
import pe.edu.upc.sad.util.ToolSql;
/**
 *
 * @author HP
 */
public class ListarReservas extends HttpServlet {

     ReservaDao tarea = new ReservaDaoImpl();

     @Override
    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {
         Gson gson = new Gson();

         List<Reserva> lstArea = null ;
        try
        {
             String idarea=(request.getParameter("idarea") != null) ?  request.getParameter("idarea").toString()  : "";
             int id_idarea=(idarea.toString().equals("")) ?   0 : Integer.parseInt(idarea);

           StringBuilder sb = new StringBuilder();

        sb.append("[{'recid':1,'subject':'Oct 2010 task 1','description':'Demo task for calendar','startdate':'10/04/2010 08:00:00 am' , 'enddate': '10/05/2010 09:45:00 am' ,'color':'#FFDF96','parent':0,'priority':1,'htmldata':'','location':'office'},");
	sb.append("{'recid':2,'subject':'Oct 2010 task 2','description':'Demo task for calendar','startdate':'10/06/2010 08:00:00 am' , 'enddate': '10/06/2010 09:45:00 am' ,'color':'#FFBDB4','parent':0,'priority':3,'htmldata':'','location':'office'},");
	sb.append("{'recid':3,'subject':'Oct 2010 task 3','description':'Demo task for calendar','startdate':'10/07/2010 08:00:00 am' , 'enddate': '10/18/2010 11:50:00 am' ,'color':'#E0FFA2','parent':0,'priority':4,'htmldata':'','location':'office'},");
	sb.append("{'recid':4,'subject':'Oct 2010 task 4','description':'Demo task for calendar','startdate':'10/09/2010 08:00:00 am' , 'enddate': '10/09/2010 10:15:00 am' ,'color':'#6C90B4','parent':0,'priority':1,'htmldata':'','location':'office'},");
	sb.append("{'recid':5,'subject':'Oct 2010 task 5','description':'Demo task for calendar','startdate':'10/15/2010 08:00:00 am' , 'enddate': '10/25/2010 10:15:00 am' ,'color':'#E0FFA2','parent':0,'priority':5,'htmldata':'','location':'office'},");
	sb.append("{'recid':6,'subject':'Oct 2010 task 6','description':'Demo task for calendar','startdate':'10/21/2010 07:00:00 am' , 'enddate': '10/21/2010 10:00:00 pm' ,'color':'#D77472','parent':1,'priority':1,'htmldata':'','location':'office'},");
	sb.append("{'recid':7,'subject':'Oct 2010 task 7','description':'Demo task for calendar','startdate':'10/20/2010 08:00:00 am' , 'enddate': '11/10/2010 09:45:00 am' ,'color':'#FFDF96','parent':1,'priority':1,'htmldata':'','location':'office'}]");

             lstArea = tarea.getListReservaByAreaEmpresa(id_idarea);
            String json = "{'Reservas':";
            json += gson.toJson(lstArea) + "}";// gson.toJson(obj);

            //response.setContentType("text/plain");
            response.setContentType("text/x-json;charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            //response.setContentLength(message.length());
            PrintWriter out = response.getWriter();
            out.println(json);

            out.close();
            out.flush();

        }catch(Exception e)
        {
            
            lstArea = new ArrayList<Reserva>();
            String json = "{'Reservas':";
            json += gson.toJson(lstArea)  + "}";

            //response.setContentType("text/plain");
            response.setContentType("text/x-json;charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            //response.setContentLength(message.length());
            PrintWriter out = response.getWriter();
            out.println(json);

            out.close();
            out.flush();
        }
    }

    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {

    }
}
